﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using A2.ERP.Common;
using A2.ERP.BLL;
using A2.ERP.IBLL;
using A2.ERP.Models;
using A2.ERP.Models.ViewModels;
using System.Drawing;

namespace A2.ERP.UI.Controllers
{
    public class HomeController : Controller
    {
        It_NoticeService t_NoticeService = new t_NoticeService();
        It_AuthorityService t_AuthorityServive = new t_AuthorityService();
        It_ServiceInfoService t_ServiceInfoService = new t_ServiceInfoService();
        It_ServiceTypeService t_ServiceTypeService = new t_ServiceTypeService();
        It_UserService t_UserService = new BLL.t_UserService();
        It_ProductsService t_ProductsServie = new BLL.t_ProductsService();
        It_ProductsTypeService t_ProductsTypeService = new BLL.t_ProductsTypeService();
        t_AppendStockDetailService t_appendService = new BLL.t_AppendStockDetailService();
        t_GroupAuthorityService _groupAuthService = new BLL.t_GroupAuthorityService();


        public ActionResult Index()
        {

            Session["uid"] = Request.Cookies["uid"].Value;
            string uid = (string)Session["uid"];
            ViewBag.user = t_UserService.LoadEntities(i => i.Token == uid).FirstOrDefault();
            List<t_Notice> notices = t_NoticeService.LoadEntities(i => true).ToList();

            ViewBag.ServiceInfo = t_ServiceInfoService.LoadEntities(i => true).ToList();

            ViewBag.Notices = notices;



            return View();
        }

        public JsonResult GetProductsStat()
        {
            var productsStat = t_ProductsTypeService.Procedures<ProductsStat>("select * from v_productsStatView");

            var data = new StatOption<ProductsStat>
            {
                Data = productsStat,
                Columns = t_ProductsTypeService.LoadEntities(i => true).Select(i => i.TypeName).ToArray()
            };
            return Json(data);
        }

        public JsonResult GetAppendStockBarStat()
        {
            var stat = (from a in t_appendService.LoadEntities(i => true)
                        join p in t_ProductsServie.LoadEntities(i => true)
                        on a.ProductsID equals p.ProductsID
                        select new AppendStockStat
                        {
                            value = a.Quantity,
                            name = p.ProductsName
                        }).ToList();

            var data = new StatOption<AppendStockStat>
            {
                Data = stat,
                Columns = stat.Select(i => i.name).ToArray()
            };

            return Json(data);
        }

        public ActionResult SignOut()
        {
             Request.Cookies["uid"].Expires = DateTime.Now.AddDays(-1);

            return Redirect("/login");
        }

        public ActionResult Menu()
        {
            string uid =(string) Session["uid"];
            int groupId = t_UserService.LoadEntities(i => i.Token == uid).FirstOrDefault().GroupID;

            ViewBag.MenuData = _groupAuthService.LoadEntities(i => i.GroupID == groupId).Select(i => i.t_Authority).ToList();


            //ViewBag.MenuData= (from a in t_AuthorityServive.LoadEntities(i => true)
            //         join b in _groupAuthService.LoadEntities(i => true) on a.AuthorityID equals b.AuthorityID
            //         where b.GroupID == 1 && b.GroupID == groupId
            //         select a).ToList();



            return PartialView();
        }

        public ActionResult NoticeDetails(int id)
        {
            ViewBag.Notice = t_NoticeService.LoadEntities(i => i.NoticeID == id).FirstOrDefault();

            return PartialView();
        }

        public ActionResult ServiceDetails(int id)
        {
            var serviceInfo = t_ServiceInfoService.LoadEntities(i => i.ID == id).FirstOrDefault();
            ViewBag.ServiceInfo = serviceInfo;
            ViewBag.ServiceType = t_ServiceTypeService.LoadEntities(i => i.TypeID == serviceInfo.TypeID).FirstOrDefault();
            return PartialView();
        }

        public ActionResult AuthError()
        {
            return View();
        }

        public ActionResult Error()
        {
            return View();
        }


    }
}
